//
//  Config.swift
//  foreign_swift
//
//  Created by hdr on 15-4-14.
//  Copyright (c) 2015年 hdr. All rights reserved.
//

import Foundation

class Config: NSObject {
    
    class var file_name:String{
        return "yolanda_config.xml"
    }
    
    class var key_email:String{
        return "email"
    }
    
    class var key_login_mark:String{
        return "login_mark"
    }
    
    class func shareInstance()->Config {
        struct YRSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:Config? = nil
        }
        dispatch_once(&YRSingleton.predicate,{
            YRSingleton.instance = Config()
            }
        )
        return YRSingleton.instance!
    }
    
    override init() {
        super.init()
        var absolutePath = FileHelper.getAbsolutePath(Config.file_name)
        
        if let _dict =  NSDictionary(contentsOfFile: absolutePath) {
            dict = _dict as! Dictionary<String,AnyObject>
        }else{
            dict = Dictionary<String,AnyObject>()
        }
    }
    

    var dict:Dictionary<String, AnyObject>!
    
    override func setValue(value: AnyObject?, forKey key: String) {
        dict[key] = value
        save()
    }
    
    func getStringValue(key:String) ->String{
        if let str = dict[key] as? String {
            return str
        }else{
            return ""
        }
    }
    
    func getIntValue(key:String) ->Int{
        if let i = dict[key] as? Int {
            return i
        }else{
            return 0
        }
    }
    
    func getBoolValueKey(key:String) ->Bool{
        if let bool = dict[key] as? Bool {
            return bool
        }else{
            return false
        }
    }
    
    private func save(){
        FileHelper.saveFile(Config.file_name, data: dict)
    }
}